//
//  ABRecordReader.m
//  Voxper
//
//  Created by Erphan on 2/22/11.
//  Copyright 2011 Smonte. All rights reserved.
//

#import "ABRecordReader.h"
#import <AddressBook/AddressBook.h>

@implementation ABRecordReader

+ (NSString*)personFirstName:(ABRecordRef)person {
	NSString *firstName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
	return [firstName autorelease];
}

+ (NSString*)personLastName:(ABRecordRef)person {
	NSString *lastName = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
	return [lastName autorelease];
}

+ (NSString*)personContactName:(ABRecordRef)person {
	NSString *firstName = [self personFirstName:person];
	NSString *lastName = [self personLastName:person];
	NSString *contactName = @"";
	
	if (firstName && [firstName length] > 0)
		contactName = [NSString stringWithString:firstName];
	
	if (lastName && [lastName length] > 0)
		contactName = [contactName stringByAppendingFormat:@" %@", lastName];
	
	return contactName;
}

+ (NSString*)personCompositeName:(ABRecordRef)person {
	NSString *fullName = (NSString *)ABRecordCopyCompositeName(person);
	return [fullName autorelease];
}

+ (NSString*)personOrganizationName:(ABRecordRef)person {
	NSString *organization = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
	return [organization autorelease];
}

+ (UIImage*)personDisplayImage:(ABRecordRef)person withThumbnail:(BOOL)isThumbnail {
	//UIImage *image = nil;
    
	if (ABPersonHasImageData(person)) {
		if (isThumbnail)
			return [UIImage imageWithData:(NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];
		else
			return [UIImage imageWithData:(NSData*)ABPersonCopyImageData(person)];
	}
	return nil;
}

+ (NSString*)escapeVCard:(NSString*)value {
	NSString *readValue = value;
	readValue = [readValue stringByReplacingOccurrencesOfString:@":" withString:@"\\:"];
	readValue = [readValue stringByReplacingOccurrencesOfString:@";" withString:@"\\;"];
	return readValue;
}

+ (NSString*)cleanNumber:(NSString*)num {
	NSString *number = [num stringByReplacingOccurrencesOfString:@" " withString:@""];
	number = [number stringByReplacingOccurrencesOfString:@"(" withString:@""];
	number = [number stringByReplacingOccurrencesOfString:@")" withString:@""];
	number = [number stringByReplacingOccurrencesOfString:@"-" withString:@""];
	number = [number stringByReplacingOccurrencesOfString:@"+" withString:@""];
	return number;
}

+ (NSArray*)personContactNumbers:(ABRecordRef)person :(BOOL)bClean {
	NSMutableArray *numbers = [NSMutableArray array];
	CFStringRef number, numberLabel;
	
	if(person != nil)
	{
		ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
		
		for (CFIndex i=0; i<ABMultiValueGetCount(phones); i++)
		{
			numberLabel = ABMultiValueCopyLabelAtIndex(phones, i);
			number      = ABMultiValueCopyValueAtIndex(phones, i);
			
			if (CFStringCompare(numberLabel, kABWorkLabel, 0) == kCFCompareEqualTo) {
				if (bClean) {
					[numbers addObject:[self cleanNumber:(NSString *)number]];
				} else {
					[numbers addObject:[self escapeVCard:(NSString *)number]];
				}
			}
			else if (CFStringCompare(numberLabel, kABHomeLabel, 0) == kCFCompareEqualTo) {
				if (bClean) {
					[numbers addObject:[self cleanNumber:(NSString *)number]];
				} else {
					[numbers addObject:[self escapeVCard:(NSString *)number]];
				}
			}
			else if(CFStringCompare(numberLabel, kABOtherLabel, 0) == kCFCompareEqualTo) {
				if (bClean) {
					[numbers addObject:[self cleanNumber:(NSString *)number]];
				} else {
					[numbers addObject:[self escapeVCard:(NSString *)number]];
				}
			}
			else if(CFStringCompare(numberLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
				if (bClean) {
					[numbers addObject:[self cleanNumber:(NSString *)number]];
				} else {
					[numbers addObject:[self escapeVCard:(NSString *)number]];
				}
			}
			else if(CFStringCompare(numberLabel, kABPersonPhoneIPhoneLabel, 0) == kCFCompareEqualTo) {
				if (bClean) {
					[numbers addObject:[self cleanNumber:(NSString *)number]];
				} else {
					[numbers addObject:[self escapeVCard:(NSString *)number]];
				}
			}
			else if(CFStringCompare(numberLabel, kABPersonPhoneMainLabel, 0) == kCFCompareEqualTo) {
				if (bClean) {
					[numbers addObject:[self cleanNumber:(NSString *)number]];
				} else {
					[numbers addObject:[self escapeVCard:(NSString *)number]];
				}
			}
			else if(CFStringCompare(numberLabel, kABPersonPhoneHomeFAXLabel, 0) == kCFCompareEqualTo) {
				if (bClean) {
					[numbers addObject:[self cleanNumber:(NSString *)number]];
				} else {
					[numbers addObject:[self escapeVCard:(NSString *)number]];
				}
			}
			else if(CFStringCompare(numberLabel, kABPersonPhoneWorkFAXLabel, 0) == kCFCompareEqualTo) {
				if (bClean) {
					[numbers addObject:[self cleanNumber:(NSString *)number]];
				} else {
					[numbers addObject:[self escapeVCard:(NSString *)number]];
				}
			}
			else if(CFStringCompare(numberLabel, kABPersonPhonePagerLabel, 0) == kCFCompareEqualTo) {
				if (bClean) {
					[numbers addObject:[self cleanNumber:(NSString *)number]];
				} else {
					[numbers addObject:[self escapeVCard:(NSString *)number]];
				}
			}
			else {
				if (bClean) {
					[numbers addObject:[self cleanNumber:(NSString *)number]];
				} else {
					[numbers addObject:[self escapeVCard:(NSString *)number]];
				}
			}
			
			CFRelease(numberLabel);
			CFRelease(number);
		}
		CFRelease(phones);
	}
	
	return numbers;
}

+ (NSArray *)personContactNumbers:(ABRecordRef)person {
	return [ABRecordReader personContactNumbers:person :NO];
}

+ (NSString*)personSelectedValue:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
	if (kABPersonPhoneProperty == property)
	{
		CFTypeRef multiValue = ABRecordCopyValue(person, property);
		CFIndex valueIndex = ABMultiValueGetIndexForIdentifier(multiValue,identifier);
        
        NSString *strReturnValue = @"";
		strReturnValue = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, valueIndex);
        
        CFRelease(multiValue);
		return [strReturnValue autorelease];
	}
	return @"";
}

+ (NSArray*)personPhoneProperties:(ABRecordRef)person {
	NSMutableArray *vCards = [NSMutableArray array];
	
	//TELEPHONE NUMBERS
	NSMutableArray *vCardInfo = [NSMutableArray array];
	CFStringRef number, numberLabel;
	ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
	
	for (CFIndex i=0; i<ABMultiValueGetCount(phones); i++)
	{
		numberLabel = ABMultiValueCopyLabelAtIndex(phones, i);
		number      = ABMultiValueCopyValueAtIndex(phones, i);
		
		if (CFStringCompare(numberLabel, kABWorkLabel, 0) == kCFCompareEqualTo)
			[vCardInfo addObject:[NSDictionary dictionaryWithObject:[self escapeVCard:(NSString *)number] forKey:@"Work"]];
		else if (CFStringCompare(numberLabel, kABHomeLabel, 0) == kCFCompareEqualTo)
			[vCardInfo addObject:[NSDictionary dictionaryWithObject:[self escapeVCard:(NSString *)number] forKey:@"Home"]];
		else if(CFStringCompare(numberLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo)
			[vCardInfo addObject:[NSDictionary dictionaryWithObject:[self escapeVCard:(NSString *)number] forKey:@"Mobile"]];
		else if(CFStringCompare(numberLabel, kABPersonPhoneIPhoneLabel, 0) == kCFCompareEqualTo)
			[vCardInfo addObject:[NSDictionary dictionaryWithObject:[self escapeVCard:(NSString *)number] forKey:@"iPhone"]];
		else if(CFStringCompare(numberLabel, kABPersonPhoneMainLabel, 0) == kCFCompareEqualTo)
			[vCardInfo addObject:[NSDictionary dictionaryWithObject:[self escapeVCard:(NSString *)number] forKey:@"Main"]];
		else if(CFStringCompare(numberLabel, kABPersonPhoneHomeFAXLabel, 0) == kCFCompareEqualTo)
			[vCardInfo addObject:[NSDictionary dictionaryWithObject:[self escapeVCard:(NSString *)number] forKey:@"Fax Home"]];
		else if(CFStringCompare(numberLabel, kABPersonPhoneWorkFAXLabel, 0) == kCFCompareEqualTo)
			[vCardInfo addObject:[NSDictionary dictionaryWithObject:[self escapeVCard:(NSString *)number] forKey:@"Fax Work"]];
		else if(CFStringCompare(numberLabel, kABPersonPhonePagerLabel, 0) == kCFCompareEqualTo)
			[vCardInfo addObject:[NSDictionary dictionaryWithObject:[self escapeVCard:(NSString *)number] forKey:@"Pager"]];
		else if(CFStringCompare(numberLabel, kABOtherLabel, 0) == kCFCompareEqualTo)
			[vCardInfo addObject:[NSDictionary dictionaryWithObject:[self escapeVCard:(NSString *)number] forKey:@"Other"]];
		else
			[vCardInfo addObject:[NSDictionary dictionaryWithObject:[self escapeVCard:(NSString *)number] forKey:@"Other"]];
		
		CFRelease(numberLabel);
		CFRelease(number);
	}
	CFRelease(phones);
	
	[vCards addObject:vCardInfo];
	return vCards;
}

@end
